package com.spatio.serving.basic.service.template.core.component.redission;

import com.spatio.serving.common.lib.common.enums.EnvironmentEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.SmartLifecycle;
import org.springframework.stereotype.Component;

import java.util.Objects;

@Component
@Slf4j
public class InitSmartLifecycleImpl implements SmartLifecycle {

    @Value("${spring.profiles.active}")
    private String env;

    @Override
    public void start() {
        try {
            log.info("+++++++++++++++++ init redisson start ++++++++++++++++");
            env = env.replace("-http", "");
            RedissonHolder.init(Objects.requireNonNull(EnvironmentEnum.valueOfEnv(env)));
            log.info("+++++++++++++++++ init redisson finish ++++++++++++++++");
        } catch (Throwable t) {
            throw new RuntimeException("InitProcessor init fail", t);
        }
    }

    @Override
    public void stop() {
        RedissonHolder.getInstance().shutdown();
    }

    @Override
    public boolean isRunning() {
        return false;
    }
}
